package edu.tgc.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

import edu.tgc.entity.system.SysUser;

public class SessionUserUtils {

    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static Object getDetails() {
        Authentication authentication = SessionUserUtils.getAuthentication();
        return authentication == null ? null : authentication.getDetails();
    }

    public static String getIp() {
        Authentication authentication = SessionUserUtils.getAuthentication();
        return authentication == null ? null : ((WebAuthenticationDetails) authentication.getDetails()).getRemoteAddress();
    }

    public static String getSessionId() {
        Authentication authentication = SessionUserUtils.getAuthentication();
        return authentication == null ? null : ((WebAuthenticationDetails) authentication.getDetails()).getSessionId();
    }

    public static SessionUser getSessionUser() {
        return (SessionUser) SessionUserUtils.getAuthentication().getPrincipal();
    }

    public static SysUser getSysUser() {
        SessionUser sessionUser = SessionUserUtils.getSessionUser();
        return sessionUser.getId() == null ? null : new SysUser(sessionUser.getId());
    }

}
